<template>
  <div class="create">
    <h2 class="img-title">添加图片</h2>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="imgRef"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="图片名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="图片地址" prop="url">
        <el-input v-model="ruleForm.url"></el-input>
      </el-form-item>
      <el-form-item label="图片类型" prop="type">
        <el-radio-group v-model="ruleForm.type">
          <el-radio label="1">轮播图</el-radio>
          <el-radio label="2">待定</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="HandleAddImgs">立即添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { addImgs } from '@/api/publics'
export default {
  data() {
    return {
      ruleForm: {
        name: '',
        url: '',
        type: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        url: [{ required: true, message: '请输入图片地址', trigger: 'blur' }],
        resource: [
          { required: true, message: '请选择图片类型资源', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    HandleAddImgs() {
      addImgs(this.ruleForm)
        .then(() => {})
        .catch(() => {})
    }
  }
}
</script>
<style lang="scss">
.create {
  .el-form {
    width: 460px;
  }
  .img-title {
    margin-top: 10px;
    margin-bottom: 40px;
    text-indent: 20px;
  }
}
</style>